From 172073cdb64acff00d7fe41b94ebc4489ba91b3c Mon Sep 17 00:00:00 2001 From: robertlipe Date: Mon, 29 Sep 2014 02:19:29 +0000 Subject: [PATCH] Checkpoint bringing some 'class' to csv_util before moving badchars. Still a terrible example of globals and pseudo ctor/dtors... --- gpsbabel/csv_util.cc | 54 ++++++++++++++++++++++++++++---------------- gpsbabel/csv_util.h | 24 ++++++++++++-------- gpsbabel/xcsv.cc | 21 +++++------------ 3 files changed, 55 insertions(+), 44 deletions(-) diff --git a/gpsbabel/csv_util.cc b/gpsbabel/csv_util.cc index 311d1a362..b71d9ec45 100644 --- a/gpsbabel/csv_util.cc +++ b/gpsbabel/csv_util.cc @@ -153,7 +153,7 @@ in_word_set(register const char* str, register unsigned int len); /****************************************************************************/ /* obligatory global struct */ /****************************************************************************/ -xcsv_file_t xcsv_file; +XcsvFile xcsv_file; extern char* xcsv_urlbase; extern char* prefer_shortnames; @@ -719,10 +719,21 @@ dec_to_human(const char* format, const char* dirs, double val) /*****************************************************************************/ /* xcsv_file_init() - prepare xcsv_file for first use. */ /*****************************************************************************/ -void -xcsv_file_init(void) +void xcsv_file_init(void) { - memset(&xcsv_file, '\0', sizeof(xcsv_file_t)); + xcsv_file.is_internal = false; + xcsv_file.prologue_lines = 0; + xcsv_file.epilogue_lines = 0; + xcsv_file.field_delimiter = QString(); + xcsv_file.field_encloser = QString(); + xcsv_file.record_delimiter = QString(); + xcsv_file.badchars = NULL; + xcsv_file.ifield_ct = 0; + xcsv_file.ofield_ct = 0; + xcsv_file.xcsvfp = NULL; + xcsv_file.fname = QString(); + xcsv_file.description = NULL; + xcsv_file.extension = NULL; QUEUE_INIT(&xcsv_file.prologue); QUEUE_INIT(&xcsv_file.epilogue); @@ -742,6 +753,10 @@ xcsv_file_init(void) xcsv_file.gps_datum = GPS_DATUM_WGS84; } +XcsvFile::XcsvFile() { + xcsv_file_init(); +} + void validate_fieldmap(field_map_t* fmp, bool is_output) { QString qkey = fmp->key; QString qval = fmp->val; @@ -1443,8 +1458,8 @@ xcsv_data_read(void) wpt_tmp = new Waypoint; s = buff; - s = csv_lineparse(s, xcsv_file.field_delimiter, - xcsv_file.field_encloser, linecount); + s = csv_lineparse(s, CSTR(xcsv_file.field_delimiter), + CSTR(xcsv_file.field_encloser), linecount); if (QUEUE_EMPTY(&xcsv_file.ifield)) { fatal(MYNAME ": attempt to read, but style '%s' has no IFIELDs in it.\n", xcsv_file.description? xcsv_file.description : "unknown"); @@ -1470,8 +1485,8 @@ xcsv_data_read(void) break; } - s = csv_lineparse(NULL, xcsv_file.field_delimiter, - xcsv_file.field_encloser, linecount); + s = csv_lineparse(NULL, CSTR(xcsv_file.field_delimiter), + CSTR(xcsv_file.field_encloser), linecount); } if ((xcsv_file.gps_datum > -1) && (xcsv_file.gps_datum != GPS_DATUM_WGS84)) { @@ -1549,7 +1564,6 @@ static void xcsv_waypt_pr(const Waypoint* wpt) { QString buff; - const char* write_delimiter; int i; field_map_t* fmp; queue* elem, *tmp; @@ -1567,7 +1581,8 @@ xcsv_waypt_pr(const Waypoint* wpt) longitude = oldlon = wpt->longitude; latitude = oldlat = wpt->latitude; - if (xcsv_file.field_delimiter && strcmp(xcsv_file.field_delimiter, "\\w") == 0) { + QString write_delimiter; + if (xcsv_file.field_delimiter == "\\w") { write_delimiter = " "; } else { write_delimiter = xcsv_file.field_delimiter; @@ -1623,7 +1638,7 @@ xcsv_waypt_pr(const Waypoint* wpt) fmp = (field_map_t*) elem; if ((i != 0) && !(fmp->options & OPTIONS_NODELIM)) { - gbfprintf(xcsv_file.xcsvfp, write_delimiter); + gbfputs(write_delimiter, xcsv_file.xcsvfp); } if (fmp->options & OPTIONS_ABSOLUTE) { @@ -2130,9 +2145,9 @@ xcsv_waypt_pr(const Waypoint* wpt) continue; } - if (xcsv_file.field_encloser) { + if (!xcsv_file.field_encloser.isEmpty()) { /* print the enclosing character(s) */ - gbfprintf(xcsv_file.xcsvfp, "%s", xcsv_file.field_encloser); + gbfputs(xcsv_file.record_delimiter, xcsv_file.xcsvfp); } /* As a special case (pronounced "horrible hack") we allow @@ -2143,13 +2158,13 @@ xcsv_waypt_pr(const Waypoint* wpt) } gbfputs(obuff, xcsv_file.xcsvfp); - if (xcsv_file.field_encloser) { + if (!xcsv_file.field_encloser.isEmpty()) { /* print the enclosing character(s) */ - gbfprintf(xcsv_file.xcsvfp, "%s", xcsv_file.field_encloser); + gbfputs(xcsv_file.record_delimiter, xcsv_file.xcsvfp); } } - gbfprintf(xcsv_file.xcsvfp, "%s", xcsv_file.record_delimiter); + gbfputs(xcsv_file.record_delimiter, xcsv_file.xcsvfp); /* increment the index counter */ waypt_out_count++; @@ -2210,8 +2225,8 @@ xcsv_data_write(void) QString t = dt.toString("hh:mm:ss"); cout.replace("__TIME__", t); } - gbfprintf(xcsv_file.xcsvfp, "%s", CSTR(cout)); - gbfprintf(xcsv_file.xcsvfp, "%s", xcsv_file.record_delimiter); + gbfputs(cout, xcsv_file.xcsvfp); + gbfputs(xcsv_file.record_delimiter, xcsv_file.xcsvfp); } if ((xcsv_file.datatype == 0) || (xcsv_file.datatype == wptdata)) { @@ -2227,7 +2242,8 @@ xcsv_data_write(void) /* output epilogue lines, if any. */ QUEUE_FOR_EACH(&xcsv_file.epilogue, elem, tmp) { ogp = (ogue_t*) elem; - gbfprintf(xcsv_file.xcsvfp, "%s%s", ogp->val, xcsv_file.record_delimiter); + gbfputs(ogp->val, xcsv_file.xcsvfp); + gbfputs(xcsv_file.record_delimiter, xcsv_file.xcsvfp); } } #endif diff --git a/gpsbabel/csv_util.h b/gpsbabel/csv_util.h index 25d8db367..533f91859 100644 --- a/gpsbabel/csv_util.h +++ b/gpsbabel/csv_util.h @@ -101,11 +101,15 @@ typedef struct char_map { } char_map_t; /* - * a type describing all the wonderful elements of xcsv files, in a + * a Class describing all the wonderful elements of xcsv files, in a * nutshell. + * It completely shows that this began life as a C struct...baby steps. */ -typedef struct { - int is_internal; /* bool - is internal (1) or parsed (0) */ +class XcsvFile { + public: + XcsvFile(); + + bool is_internal; /* bool - is internal (1) or parsed (0) */ int prologue_lines; /* # of lines to ignore at top of the file */ int epilogue_lines; /* # of lines to ignore at bottom of file */ @@ -116,11 +120,11 @@ typedef struct { /* footer lines for writing at the bottom of the file. */ queue epilogue; - char* field_delimiter; /* comma, quote, etc... */ - char* field_encloser; /* doublequote, etc... */ - char* record_delimiter; /* newline, c/r, etc... */ + QString field_delimiter; /* comma, quote, etc... */ + QString field_encloser; /* doublequote, etc... */ + QString record_delimiter; /* newline, c/r, etc... */ - char* badchars; /* characters we never write to output */ + char* badchars; /* characters we never write to output */ queue ifield; /* input field mapping */ queue* ofield; /* output field mapping */ @@ -129,7 +133,7 @@ typedef struct { int ofield_ct; /* actual # of ofields */ gbfile* xcsvfp; /* ptr to current *open* data file */ - char* fname; /* ptr to filename of above. */ + QString fname; /* ptr to filename of above. */ char* description; /* Description for help text */ char* extension; /* preferred filename extension (for wrappers)*/ @@ -141,10 +145,10 @@ typedef struct { gpsdata_type datatype; /* can be wptdata, rtedata or trkdata */ /* ... or ZERO to keep the old behaviour */ -} xcsv_file_t; +}; /****************************************************************************/ /* obligatory global struct */ /****************************************************************************/ -extern xcsv_file_t xcsv_file; +extern XcsvFile xcsv_file; diff --git a/gpsbabel/xcsv.cc b/gpsbabel/xcsv.cc index 88cf49ea5..da3e516c4 100644 --- a/gpsbabel/xcsv.cc +++ b/gpsbabel/xcsv.cc @@ -177,17 +177,9 @@ xcsv_destroy_style(void) } /* other alloc'd glory */ - if (xcsv_file.field_delimiter) { - xfree(xcsv_file.field_delimiter); - } - - if (xcsv_file.field_encloser) { - xfree(xcsv_file.field_encloser); - } - - if (xcsv_file.record_delimiter) { - xfree(xcsv_file.record_delimiter); - } + xcsv_file.field_delimiter = QString(); + xcsv_file.field_encloser = QString(); + xcsv_file.record_delimiter = QString(); if (xcsv_file.badchars) { xfree(xcsv_file.badchars); @@ -207,7 +199,6 @@ xcsv_destroy_style(void) /* return everything to zeros */ internal = xcsv_file.is_internal; - memset(&xcsv_file, '\0', sizeof(xcsv_file)); xcsv_file.is_internal = internal; } @@ -259,7 +250,7 @@ xcsv_parse_style_line(char* sbuff) xcsv_file.field_delimiter = sp; } - p = csv_stringtrim(xcsv_file.field_delimiter, " ", 0); + p = csv_stringtrim(CSTR(xcsv_file.field_delimiter), " ", 0); /* field delimiters are always bad characters */ if (0 == strcmp(p, "\\w")) { @@ -286,7 +277,7 @@ xcsv_parse_style_line(char* sbuff) xcsv_file.field_encloser = sp; } - p = csv_stringtrim(xcsv_file.field_encloser, " ", 0); + p = csv_stringtrim(CSTR(xcsv_file.field_encloser), " ", 0); /* field_enclosers are always bad characters */ if (xcsv_file.badchars) { @@ -313,7 +304,7 @@ xcsv_parse_style_line(char* sbuff) xcsv_file.record_delimiter = sp; } - p = csv_stringtrim(xcsv_file.record_delimiter, " ", 0); + p = csv_stringtrim(CSTR(xcsv_file.record_delimiter), " ", 0); /* record delimiters are always bad characters */ if (xcsv_file.badchars) { -- 2.30.2